﻿#region --- License & Copyright Notice
/*
CRUD Services Library

Copyright 2012 Jeevan James

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Json;
using System.Text;

namespace CrudServices.WebApi.Client
{
    public class CrudClientWebApi : ICrudService
    {
        private readonly HttpClient _client;
        private readonly string _rootUri;

        public CrudClientWebApi(string rootUrl)
        {
            _client = new HttpClient();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _client.DefaultRequestHeaders.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
            _rootUri = rootUrl.EndsWith("/") ? rootUrl : rootUrl + "/";
        }

        public CrudClientWebApi(Uri rootUri) : this(rootUri.ToString())
        {
        }

        public string Search(SearchCriteria criteria)
        {
            string url = string.Format("{0}{1}", _rootUri, criteria.Source);
            string result = _client.GetStringAsync(url).Result;
            return Deserialize(result);
        }

        public string Find(string entityName, int id)
        {
            string url = string.Format("{0}{1}/{2}", _rootUri, entityName, id);
            string result = _client.GetStringAsync(url).Result;
            return Deserialize(result);
        }

        public string Add(string entityName, string items)
        {
            throw new NotImplementedException();
        }

        public int Update(string items)
        {
            throw new NotImplementedException();
        }

        public int Delete(string items)
        {
            throw new NotImplementedException();
        }

        public int DeleteIds(string name, IEnumerable<int> itemIds)
        {
            throw new NotImplementedException();
        }

        public string SaveChanges(string items)
        {
            throw new NotImplementedException();
        }

        private static string Deserialize(string json)
        {
            var deserializer = new DataContractJsonSerializer(typeof(string));
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                return (string)deserializer.ReadObject(stream);
        }
    }
}